package com.dl.mall.config;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.env.Environment;
import org.springframework.core.env.Profiles;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.service.Contact;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

import java.util.ArrayList;

/**
 * swagger2配置类
 * @author DL
 * @create 2021-01-30 10:11
 */
@Configuration
@EnableSwagger2//开启swagger2
public class SwaggerConfig {

    @Value("${swagger.enable}")
    public boolean swaggerEnable = false;

/*    @Bean
    public Docket docket(Environment environment){

        //配置swaggerDocket实例
        return new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(apiInfo())
                .enable(swaggerEnable)//是否启用swagger
                .groupName("DL")
                //RequestHandlerSelectors：配置要扫描的包
                //basePackage("com.dl.controller")：指定要扫描的包
                //any()：扫描全部
                //none()：不扫描
                //withClassAnnotation()：扫描类上的注解，参数是一个注解的反射对象
                //withMethodAnnotation()：扫描方法上的注解
                //.select().apis(RequestHandlerSelectors.withClassAnnotation(RestController.class)).build();
                //.select().apis(RequestHandlerSelectors.basePackage("com.dl.controller"))
                .select()
                .apis(RequestHandlerSelectors.withClassAnnotation(Api.class))          //只扫描有api注解的类
                .apis(RequestHandlerSelectors.withMethodAnnotation(ApiOperation.class))//只扫描有ApiOperation注解的方法
                //过滤什么路径
                //.paths(PathSelectors.ant("/dl/**"))
                .build();
    }*/

    @Bean
    public Docket userSwagger() {
        return new Docket(DocumentationType.SWAGGER_2).enable(swaggerEnable)
                .apiInfo(frontApiInfo()).select()
                .apis(RequestHandlerSelectors.basePackage("com.dl.mall.controller.user"))
                .paths(PathSelectors.any()).build().groupName("前台接口").pathMapping("/");
    }

    @Bean
    public Docket adminSwagger() {
        return new Docket(DocumentationType.SWAGGER_2).enable(swaggerEnable)
                .apiInfo(adminApiInfo()).select()
                .apis(RequestHandlerSelectors.basePackage("com.dl.mall.controller.admin"))
                .paths(PathSelectors.any()).build().groupName("后台接口").pathMapping("/");
    }


    //配置swagger信息
    private ApiInfo frontApiInfo(){
        //作者信息
        Contact DEFAULT_CONTACT = new Contact("丁亮", "http://localhost:8088/", "2458697735@qq.com");
        return new ApiInfo(
                "前台API文档",
                "这个作者有点酷",
                "v1.0",
                "http://localhost:8080/",
                DEFAULT_CONTACT,
                "Apache 2.0",
                "http://www.apache.org/licenses/LICENSE-2.0",
                new ArrayList());
    }

    //配置swagger信息
    private ApiInfo adminApiInfo(){
        //作者信息
        Contact DEFAULT_CONTACT = new Contact("丁亮", "http://localhost:8088/", "2458697735@qq.com");
        return new ApiInfo(
                "后台API文档",
                "这个作者有点酷",
                "v1.0",
                "http://localhost:8080/",
                DEFAULT_CONTACT,
                "Apache 2.0",
                "http://www.apache.org/licenses/LICENSE-2.0",
                new ArrayList());
    }

}
